/**
 * 点位编辑操作集
 */
import { computed } from 'vue'
import { UseModel } from '@/stores/model'
import { ElMessage, ElMessageBox } from 'element-plus'
import { savePoint } from '@/api/equipment'
const model = UseModel()//表示大屏模式，看板or编辑
interface point {
    equipmentId: string,//设备id
    name: string,//设备名称
    locationId: number,//区域id，为前端自定义
    position: { top: string, left: string },//设备位置
    location: string,//区域名称
    wsMsg: {}[],//告警信息
    popoverVisible: boolean,//是否显示弹窗
    status: string,//设备状态
    equipmentLocationCode: string,//设备唯一标识
    editId: number,//设备编辑id
    id: number,//id
    type: string//设备位置
}
/**
 * 大屏模式：
 * dashboard：看板模式
 * edit：编辑模式
 */
const Model = computed(() => {
    return model.$state.model
})
/**
 * 编辑模式：true表示已选择设备类型，false代表否
 */
const pointEdit = computed(() => {
    return model.$state.edit
})
/**
 * 
 * @param event 事件点击参数
 * @param pointData 设备点位数据
 * @param selectEqType 选择点位的类型
 * @returns 
 */
export const edit = (event, pointData: Array<point>, selectEqType: string) => {
    if (Model.value == 'edit' && pointEdit.value) {
        console.log(event.target);
        if (event.target.id != 'layout') {
            ElMessage.warning('在这附近已经有点位了，请重新选择！')
            var body = document.querySelector("body")
            body.style.cursor = "default"
            model.Setedit(false)
            return
        }
        let left = Math.floor((event.x / innerWidth) * 100) + '%'
        let top = Math.floor((event.y / innerHeight) * 100) + '%'
        let obj = {
            equipmentId: '', name: '', locationId: 1, position: { top: top, left: left }, location: '', wsMsg: [{

            }], popoverVisible: true, status: 'onLine', equipmentLocationCode: '', editId: (event.x * event.y), id: null, type: selectEqType
        }
        pointData.push(obj)
        console.log(event.x, event.y, '位置信息');
        var body = document.querySelector("body")
        body.style.cursor = "default"
        model.Setedit(false)
        selectEqType = ''

    }
}

/**
 * 
 * @param id 编辑id
 * @param pointData 设备点位数据 
 */
export const Editcancel = (id, pointData: Array<point>) => {
    if (Model.value == 'edit') {
        console.log(id);
        let index = pointData.findIndex(item => item.editId && item.editId == id)
        if (index > -1) {
            pointData.splice(index, 1)
        }
    }
}

/**
 * 
 * @param code 设备唯一标识
 * @param pointData 设备点位数据
 * @param cb 回调函数：判断区域下是否存在告警的点位
 * @param cb1 回调函数：设置下拉菜单选项是否已选
 */
export const delectPoint = async (code: string, pointData: Array<point>, cb: Function, cb1: void) => {
    await ElMessageBox.confirm(`是否删除设备：${code}？一但确认无法取消！`, '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        ElMessage({
            type: 'success',
            message: '删除成功',
        })
        pointData.forEach((item, index) => {
            if (item.equipmentLocationCode == code) {
                pointData.splice(index, 1)
            }
        })
        // pointData = pointData.filter(item => {
        //     return item.equipmentLocationCode != code
        // })
        cb()
        cb1

    }).catch(() => {

    })
}

/**
 * 
 * @param pointData 设备点位数据
 * @param cb 回调函数：初始化点位数据
 * @returns 
 */
export const saveEdit = (pointData: Array<point>, cb: Function) => {
    if (Model.value == 'edit') {
        if (pointData.some(item => !item.equipmentLocationCode || item.equipmentLocationCode == '')) {

            ElMessage.warning('存在没有配置的点位，请进行配置!')
            return
        } else {
            ElMessageBox.confirm(`是否保存配置？`, '点位配置', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'success',
            }).then(async () => {
                let data = []
                pointData.forEach((item) => {
                    let eqObj = { equipmentLocationCode: item.equipmentLocationCode, position: item.position, locationId: item.locationId, id: item.id }
                    data.push(eqObj)
                })
                console.log('保存的数据---------------------------', data);
                await savePoint(data).then(() => {
                    ElMessage.success('保存配置成功，即将进行跳转！')
                })
                await cb()
                model.SetMdel('dashboard')
            }).catch(() => {
                return
            }).finally(() => {
                var body = document.querySelector("body")
                body.style.cursor = "default"
                model.Setedit(false)
            })

        }

    }
}

/**
 * 
 * @param pointData 设备点位数据
 * @param cb 回调函数：初始化点位数据
 */
export const exitEdit = async (pointData: Array<point>, cb: Function) => {
    pointData.forEach((item, index) => {
        if (item.editId) {
            if (index > -1) {
                pointData.splice(index, 1)
            }
        }
    })
    var body = document.querySelector("body")
    body.style.cursor = "default"
    await cb()
    model.SetMdel('dashboard')
    model.Setedit(false)
}